package com.example.friend.mvp.ui.activity;

import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.friend.R;
import com.example.friend.mvp.ui.util.GlideSimpleLoader;
import com.example.friend.mvp.ui.util.Utils;
import com.example.friend.mvp.ui.weight.CustomPopupWindow;
import com.github.ielse.imagewatcher.ImageWatcherHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: admins
 * date:   2019/6/28 10:58
 * desc:
 */
public class ImagePreViewActivity extends AppCompatActivity implements CustomPopupWindow.ViewInterface {
    private ImageView imageView;
    private CustomPopupWindow popupWindow;
    private ImageWatcherHelper iwHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        boolean isTranslucentStatus = false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
            isTranslucentStatus = true;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_preview);
        initView(isTranslucentStatus);
        initListener();
    }


    private void initView(boolean isTranslucentStatus) {
        imageView = findViewById(R.id.iv_preview);
        iwHelper = ImageWatcherHelper.with(this,new GlideSimpleLoader()) // 一般来讲， ImageWatcher 需要占据全屏的位置
                .setTranslucentStatus(!isTranslucentStatus ? Utils.calcStatusBarHeight(this) : 0) // 如果不是透明状态栏，你需要给ImageWatcher标记 一个偏移值，以修正点击ImageView查看的启动动画的Y轴起点的不正确
                .setErrorImageRes(R.mipmap.error_picture); // 配置error图标 如果不介意使用lib自带的图标，并不一定要调用这个API

    }

    private void initListener() {
        final String picThumbUrl = "http://img.hb.aicdn.com/df75487fbc3ad89bb0c42eee053c3fd2fdc6b18a12b99-qYfZrA_fw658";
        final String picUrl = "http://img.hb.aicdn.com/adf79cb0385bfe98456fea7768b12a5ca4eca0b8c81b-Tp0ywW_fw658";
        Glide.with(imageView.getContext()).asBitmap().load(picUrl).into(imageView);
        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showSharePopup();
                return false;
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final List<String> longPictureList = new ArrayList<>();
                longPictureList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536811355543&di=87e5c3def99fdad3e356263114f11f0b&imgtype=0&src=http%3A%2F%2Fthumb11.jfcdns.com%2Fthumb%2F2017-11%2Fbce5a02c4551db77_600_400.jpeg");
                longPictureList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536811355543&di=a0dbe4cb9420b031669693f2d170aaae&imgtype=0&src=http%3A%2F%2Fthumb10.jfcdns.com%2Fthumb%2F2017-11%2Fbce5a02c45735cc6_600_400.jpeg");
                longPictureList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536811355543&di=a1b77f149342811754180a7b88382e13&imgtype=0&src=http%3A%2F%2Fgss0.bdstatic.com%2F7LsWdDW5_xN3otebn9fN2DJv%2Fdoc%2Fpic%2Fitem%2Ff31fbe096b63f62442fe1e3b8b44ebf81a4ca362.jpg");
                longPictureList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536811355545&di=2436088f354f1a8355ea8db0a82592e0&imgtype=0&src=http%3A%2F%2Fgss0.bdstatic.com%2F7LsWdDW5_xN3otebn9fN2DJv%2Fdoc%2Fpic%2Fitem%2F3801213fb80e7bec392a30d9232eb9389b506b11.jpg");
                final int initPosition = 0;
                final SparseArray<ImageView> mappingViews = new SparseArray<>();
                mappingViews.put(initPosition, (ImageView) v);
                iwHelper.show(initPosition, mappingViews, convert(longPictureList));
            }
        });
    }

    private void showSharePopup() {
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        popupWindow = new CustomPopupWindow.Builder(this)
                .setView(R.layout.dialog_share)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setAnimationStyle(R.style.anim_push_bottom)
                .setBackGroundLevel(0.5f)
                .setViewOnclickListener(this)
                .setOutsideTouchable(true)
                .create();
        popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void getChildView(View view, int layoutResId) {

    }


    private List<Uri> convert(List<String> data) {
        List<Uri> list = new ArrayList<>();
        for (String d : data) list.add(Uri.parse(d));
        return list;
    }

}
